<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Service\Facades\Pay;
use App\Service\Model\Orders;
use App\Service\Model\Cards;

class PayController extends BaseController
{

    
    public function quickpass(Request $request){
        $amount  = $request->amount;
        $cardno  = $request->cardno;
        $card    = Cards::getCardByNo($cardno);
        $agreeid = $card->agreeid;
        $cusid   = $card->cusid;
        $acctname= $card->acctname;
        $orderId = generate_orderId('QP');
        $fee     = 0;
        Orders::createOrder($cardno,$orderId,$amount,Orders::type_pay ,$acctname);
        $ret     = Pay::quickpass($orderId , $agreeid ,$amount , $fee , $cusid );
        
        if(Pay::check($ret)){
            return $this->success();
        }
        return $this->failed($ret->retmsg);
    }
    /**
     * 查询余额
     *
     * @return void
     */
    public function queryBalance(){
        /** @var User $user */
        $user     =   user();
        $cusid    =   $user->cusid;
        
        $ret      =   Pay::queryBalance($cusid);
        if(Pay::check($ret)){
            $user->balance = $ret->balance;
            $user->save();
            return $this->success($ret);    
        }else{
            return $this->failed($ret->retmsg);
        }
    }

    
    public function updatesettinfo(){
        /** @var User $user */
        $user     =   user();
        $cusid    =   $user->cusid;

        $prodlist =   "[{'trxcode':'QUICKPAY_NOSMS','feerate':'0.80'},{'trxcode':'TRX_PAY','feerate':'1'}]";
        $ret      =   Pay::updatesettinfo($cusid , $prodlist);
        if(Pay::check($ret)){
            return $this->success($ret);
        }else{
            return $this->failed($ret->retmsg);
        }
    } 

    public function withdraw(){
        /** @var User $user */
        $user     =   user();
        $cusid    =   $user->cusid;
        $amount   =   '99';
        $orderId  =   generate_orderId('W');
        $ret      =   Pay::withdraw($cusid , $amount, $orderId , 1);
        if(Pay::check($ret)){
            return $this->success($ret);
        }else{
            return $this->failed($ret->retmsg);
        }
    }
}
